package com.hgj.dao.impl;

import com.hgj.dao.ShopCartDao;
import com.hgj.entity.Shopcart;
import com.hgj.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class ShopCartDaoImpl implements ShopCartDao {
    Connection con=DBUtil.getConnection();
    QueryRunner qr=new QueryRunner();
    @Override
    public int save(Shopcart obj) {
        String sql = "INSERT INTO  tb_ShopCart(ShopCart_id,user_id,goods_id,ShopCart_num,ShopCart_status) VALUES (?,?,?,?,?)";
        Object[] para = {obj.getShopcart_id(), obj.getUser_id(), obj.getGoods_id(), obj.getShopcart_num(),
                obj.getShopcart_status()};
        try {
            int row = qr.update(con, sql, para);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Shopcart obj) {
        String sql = "UPDATE  tb_shopcart SET  user_id=?,goods_id=?,shopcart_num=?,shopcart_status=? WHERE shopcart_id=? ";
        Object[] para = {obj.getUser_id(), obj.getGoods_id(), obj.getShopcart_num(),
                obj.getShopcart_status(), obj.getShopcart_id()};
        try {
            int row = qr.update(con, sql, para);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql = "DELETE FROM tb_shopcart WHERE shopcart_id  = ?  ";
        Object[] para = {id};
        try {
            int row = qr.update(con, sql, para);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Shopcart findById(Object id) {
        return null;
    }

    @Override
    public List<Shopcart> findAll() {
        return null;
    }

    @Override
    public List<Shopcart> findSearch(String keyword) {
        return null;
    }

    @Override
    public List<Shopcart> findPage(int page, int pagesize) {
        return null;
    }

    @Override
    public long count() {
        return 0;
    }

    @Override
    public List<Shopcart> findList(Map<String, Object> map) {
        String sql = "select sc.*,g.goods_name,g.goods_img,u.user_name from tb_ShopCart sc,tb_user u,tb_goods g  where u.user_id=sc.user_id and g.goods_id=sc.goods_id ";
        Object[] para = {};
        if (map.get("user_id") != null) {
            sql = sql + " and   sc.user_id=? ";
            para = new Object[]{map.get("user_id").toString()};
        }
        try {
            List<Map<String, Object>> list = qr.query(con, sql, para, new MapListHandler());
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Map<String, Object>> findMapList(Map<String, Object> map) {
        String sql = "select * from tb_ShopCart  where 1=1 ";
        Object[] para = {};
        if (map.get("goods_id") != null && map.get("goods_id") != null && map.get("shopcart_status") != null) {
            sql = sql + " and   goods_id =?  and  user_id=? and shopcart_status=? ";
            para = new Object[]{map.get("goods_id").toString(), map.get("user_id").toString(), map.get("shopcart_status").toString()};
        }
        try {
            List<Shopcart> shopCartList = qr.query(con, sql, para, new BeanListHandler<Shopcart>(Shopcart.class));
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
